import WebSocket from 'ws'
import { Observable, merge, fromEvent } from 'rxjs'

export function createWebSocketServer(options?: WebSocket.ServerOptions): Observable<any> {
  const ws = new WebSocket.Server(options, () => {
    console.log('web socket created!')
  })
  const connection = fromEvent(ws, 'connection').pipe()
  const error = fromEvent(ws, 'error').pipe()
  const headers = fromEvent(ws, 'headers').pipe()
  const listening = fromEvent(ws, 'listening').pipe()
  return merge(connection, error, headers, listening)
}
